function varargout = imconver(varargin)
% IMCONVER M-file for imconver.fig
%      IMCONVER, by itself, creates a new IMCONVER or raises the existing
%      singleton*.
%
%      H = IMCONVER returns the handle to a new IMCONVER or the handle to
%      the existing singleton*.
%
%      IMCONVER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in IMCONVER.M with the given input arguments.
%
%      IMCONVER('Property','Value',...) creates a new IMCONVER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before imconver_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to imconver_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help imconver

% Last Modified by GUIDE v2.5 15-May-2012 15:45:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @imconver_OpeningFcn, ...
                   'gui_OutputFcn',  @imconver_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before imconver is made visible.
function imconver_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to imconver (see VARARGIN)

% Choose default command line output for imconver
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes imconver wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = imconver_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
global sofile data1 destination;
sofile=0;
data1={};
set(handles.uitable1, 'data',data1);
destination=get(handles.edit1,'string');
% --- Executes on button press in add1.
function add1_Callback(hObject, eventdata, handles)
% hObject    handle to add1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[inputfile, location]=uigetfile({'*.bmp;*.jpg;*.jpeg;*.tiff;*.tif;*.png;*.gif;*.ppm','Image files';...
                                 '*.bmp','Windows bitmap(*.bmp)';...
                                 '*.jpg;*.jpeg','Joint Photographic Experts Group(*.jpg,*.jpeg)';...
                                 '*.tiff;*.tif','Tagged Image File Format(*.tiff,*.tif)';...
                                 '*.ppm','Portable Pixmap(*.ppm)';...
                                 '*.pnm','Portable Anymap(*.pnm)';...
                                 '*.png','Portable Network Graphics(*.png)';...
                                 '*.gif','Graphics Interchange Format(*.gif)'},...                                 
                                 'Add file');
if location~=0    
    global sofile data1;
    A=strfind(inputfile,'.');
    B=A(end);
    name=inputfile(1:B-1);
    outputfile=strcat(name,'.bmp');
    outfile=strcat(get(handles.edit1,'string'),'\',outputfile);
    infile=strcat(location,'\',inputfile);
    if sofile==0
        C=exist(outfile,'file');
        if C==0
            data1={true,inputfile,location,outputfile,'bmp','Waiting...'};
        else
            outputfile=strcat(name,'1','.bmp');
            data1={true,inputfile,location,outputfile,'bmp','Waiting...'};
        end
        D=imread(strcat(location,inputfile));
        if isdir('C:\temp')==0
            mkdir('C:\temp');
        end
        imwrite(D,strcat('C:\temp\',outputfile),'bmp');
        set(handles.uitable1, 'data',data1);
        sofile=sofile+1;
    else
        tmp=strcat(location,inputfile);
        data1=get(handles.uitable1, 'data');
        kichthuoc=size(data1);
        them=1;
        for i=1:(kichthuoc(1,1))
            tmp1=strcat(data1(i,3),data1(i,2));
            if strcmp(tmp,tmp1)==1
                them=0;
            end
        end
        if them==0
            existfile;
        else
            C=exist(outfile,'file');
            if C==0
                them1={true,inputfile,location,outputfile,'bmp','Waiting...'};
            else
                outputfile=strcat(name,'1','.bmp');
                them1={true,inputfile,location,outputfile,'bmp','Waiting...'};
            end
            D=imread(strcat(location,inputfile));
            imwrite(D,strcat('C:\temp\',outputfile),'bmp');
            data1=vertcat(data1,them1);
            set(handles.uitable1, 'data',data1);
            sofile=sofile+1;
        end
    end
end

% --- Executes on button press in remove1.
function remove1_Callback(hObject, eventdata, handles)
% hObject    handle to remove1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
persistent sofileremove bangphu1;
global sofile data1;
if isempty(sofileremove)
    sofileremove=0;
end
data1=get(handles.uitable1, 'data');
kichthuoc=size(data1);
for i=1:(kichthuoc(1,1))
    if cell2mat(data1(i,1))==1
        sofileremove=sofileremove+1;
    end
end

if sofileremove~=0
    kichthuoc=size(data1);
    bangphu1={};
    for i=1:(kichthuoc(1,1))
        if cell2mat(data1(i,1))==0
            bangphu1=vertcat(bangphu1,data1(i,:));
        end
    end
    data1=bangphu1;
    set(handles.uitable1,'data',data1);
    sofile=sofile-sofileremove;
else
    selectfile;
end

% --- Executes on button press in clear1.
function clear1_Callback(hObject, eventdata, handles)
% hObject    handle to clear1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global sofile data1;
sofile = 0;
data1={};
data2={};
set(handles.uitable1,'data',data1);
set(handles.uitable2,'data',data2);
cla(handles.axes1,'reset');
cla(handles.axes2,'reset');

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data1 destination;
destination=get(handles.edit1,'string');
data1=get(handles.uitable1, 'data');
kichthuoc=size(data1);
for i=1:(kichthuoc(1,1))
    a=strfind(cell2mat(data1(i,2)),'.');
    b=a(end);
    out=cell2mat(data1(i,2));
    outname=out(1:b-1);
    outfile=strcat(destination,'\',outname,'.',cell2mat(data1(i,5)));
    C=exist(outfile,'file');
    if C==0
        data1(i,4)=mat2cell(strcat(outname,'.',cell2mat(data1(i,5))));
    else
        data1(i,4)=mat2cell(strcat(outname,'1.',cell2mat(data1(i,5))));        
    end
end
set(handles.uitable1, 'data',data1);

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'string',pwd);

% --- Executes on button press in browser1.
function browser1_Callback(hObject, eventdata, handles)
% hObject    handle to browser1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data1 destination;
destination=uigetdir(pwd);
if destination~=0
    set(handles.edit1,'string',destination);
end
data1=get(handles.uitable1, 'data');
kichthuoc=size(data1);
for i=1:(kichthuoc(1,1))
    a=strfind(cell2mat(data1(i,2)),'.');
    b=a(end);
    out=cell2mat(data1(i,2));
    outname=out(1:b-1);
    outfile=strcat(destination,'\',outname,'.',cell2mat(data1(i,5)));
    C=exist(outfile,'file');
    if C==0
        data1(i,4)=mat2cell(strcat(outname,'.',cell2mat(data1(i,5))));
    else
        data1(i,4)=mat2cell(strcat(outname,'1.',cell2mat(data1(i,5))));        
    end
end
set(handles.uitable1, 'data',data1);

% --- Executes on button press in encode1.
function encode1_Callback(hObject, eventdata, handles)
% hObject    handle to encode1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
persistent sofileconver;
global data1;
if isempty(sofileconver)
    sofileconver=0;
end
data1=get(handles.uitable1, 'data');
kichthuoc=size(data1);
for i=1:(kichthuoc(1,1))
    if cell2mat(data1(i,1))==1
        sofileconver=sofileconver+1;
    end
end
if sofileconver==0
    selectfile;
else
    for i=1:(kichthuoc(1,1))
        if cell2mat(data1(i,1))==1
            C=exist(strcat('C:\temp\',cell2mat(data1(i,4))),'file');
            if C~=0
            movefile(strcat('C:\temp\',cell2mat(data1(i,4))),strcat(get(handles.edit1,'string'),'\'));
            end
            data1(i,6)=mat2cell('Finish',1);
        end
        set(handles.uitable1,'data',data1); 
    end
    if isdir('C:\temp')==1
        rmdir('C:\temp','s');
    end
    done
end


% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)
global data1 sofile;
data1=get(handles.uitable1,'data');
kichthuoc=size(data1);
if isdir('C:\temp')==0
    mkdir('C:\temp','s');
end
if sofile~=0
    for i=1:(kichthuoc(1,1))
        a=strfind(cell2mat(data1(i,4)),'.');
        if size(a)~=0
            b=a(end);
            out=cell2mat(data1(i,4));
            outname=out(1:b-1);
        else
            outname=cell2mat(data1(i,4));
        end
        data1(i,4)=mat2cell(strcat(outname,'.',cell2mat(data1(i,5))),1);
        data1(i,6)=mat2cell('Waiting...',1);
        tmp=strcat(data1(i,3),data1(i,2));           
        D=imread(cell2mat(tmp));
        imwrite(D,strcat('C:\temp\',cell2mat(data1(i,4))),cell2mat(data1(i,5)));
    end
    set(handles.uitable1,'data',data1);
end

% --- Executes when selected cell(s) is changed in uitable1.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)
global data1 destination sofile;
if sofile~=0
    sel = eventdata.Indices;
    select = unique(sel(:,1));
    % Don't process less than a minimum nuber of observations
    if size(select)==1
        data1=get(handles.uitable1,'data');
        row=sel(1,1);
        in=strcat(cell2mat(data1(row,3)),cell2mat(data1(row,2)));
        out=strcat('C:\temp\',cell2mat(data1(row,4)));
        axes(handles.axes1);
        imshow(in);
        axes(handles.axes2);
        imshow(out);
        ininfo=imfinfo(in);
        outinfo=imfinfo(out);
        data2=vertcat({ininfo.Filename,strcat(destination,'\',cell2mat(data1(row,4)))},...
           {ininfo.Format,outinfo.Format},...
           {ininfo.FileSize,outinfo.FileSize},...
           {ininfo.Width,outinfo.Width},...
           {ininfo.Height,outinfo.Height},...
           {ininfo.BitDepth,outinfo.BitDepth});
        set(handles.uitable2,'data',data2);
    end
end

% --- Executes on key press with focus on uitable1 and none of its controls.
function uitable1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
rmdir('C:\temp','s');
delete(hObject);